var region = new Object();


region.loadRegions = function(parent, type, target) {
  //Ajax.call(region.getFileName(), 'type=' + type + '&target=' + target + "&parent=" + parent , region.response, "GET", "JSON");

  $.getJSON(BASE_URI + "regions/load_regions?" + 'type=' + type + '&target=' + target + "&parent=" + parent, function(res) {
    if ('undefined' == res || res.error > 0) {
      alert('Error loading:[regions]');
    } else {
      region.response(res);
    }
  });

}

/* *
 * 载入指定的国家下所有的省份
 *
 * @country integer     国家的编号
 * @selName string      列表框的名称
 */
region.loadProvinces = function(country, selName) {
  var objName = (typeof selName == "undefined") ? "selProvinces" : selName;

  region.loadRegions(country, 1, objName);
}

/* *
 * 载入指定的省份下所有的城市
 *
 * @province    integer 省份的编号
 * @selName     string  列表框的名称
 */
region.loadCities = function(province, selName) {
  var objName = (typeof selName == "undefined") ? "selCities" : selName;

  region.loadRegions(province, 2, objName);
}

/* *
 * 载入指定的城市下的区 / 县
 *
 * @city    integer     城市的编号
 * @selName string      列表框的名称
 */
region.loadDistricts = function(city, selName) {
  var objName = (typeof selName == "undefined") ? "selDistricts" : selName;

  region.loadRegions(city, 3, objName);
}

/* *
 * 处理下拉列表改变的函数
 *
 * @obj     object  下拉列表
 * @type    integer 类型
 * @selName string  目标列表框的名称
 */
region.changed = function(obj, type, selName, selparname) {
  var parent = obj.options[obj.selectedIndex].value;
  if(type == 2){
	  if(parent > 0){
		  var isMunic = $(obj).children('option:selected').attr("munic");
		  isMunic = typeof(isMunic) == 'undefined' ? 0 : isMunic;
		  if(isMunic > 0){
			  $("#" + selName + ' option').remove();
			  $("<option selected value = '" + parent + "'>" + obj.options[obj.selectedIndex].text + "</option>").appendTo($("#" + selName));
			  this.loadRegions(parent, type, selparname);
		  }else{
			  $("#" + selparname + ' option').not(':eq(0)').remove();
		  	  this.loadCities(parent, selName);
		  }
	  }else{
		  $("#" + selName + ' option').not(':eq(0)').remove();
		  $("#" + selparname + ' option').not(':eq(0)').remove();
	  }
  }else if(type == 3){
	  if(parent > 0){
	  	this.loadDistricts(parent, selName);
	  }else{
		//$("#" + selparname + "_div").hide();
		$("#" + selparname + ' option').not(':eq(0)').remove();
	  }
  }
  
  //if (parent == '' && type == 2) {
//    $("#" + selName).html('');
//    $("<option selected value = ''>--请选择--</option>").appendTo($("#" + selName));
//    $("#" + selparname).html('');
//    $("<option selected value = ''>--请选择--</option>").appendTo($("#" + selparname));
//  } else if (parent == '' && type == 3) {
//    $("#" + selName).html('');
//    $("<option selected value = ''>--请选择--</option>").appendTo($("#" + selName));
//  } else {
//    //直辖市特殊处理
//    if (parent == 2 || parent == 25 || parent == 27 || parent == 32) {
//      if (type == 2) {
//        $("#" + selName).html('');
//        $("<option selected value = '" + parent + "'>" + obj.options[obj.selectedIndex].text + "</option>").appendTo($("#" + selName));
//        region.loadRegions(parent, type, selparname);
//      }
//    } else {
//      if (type == 2) {
//        $("#" + selparname).html('');
//        $("<option selected value = ''>--请选择--</option>").appendTo($("#" + selparname));
//      }
//      region.loadRegions(parent, type, selName);
//    }
//  }
}
region.getRegion = function(parent, type, selName, selectedId) {
  var parent = obj.options[obj.selectedIndex].value;
  region.loadRegions(parent, type, selName);
  //var target = selName;
  //Ajax.call(region.getFileName(), 'type=' + type + '&target=' + target + "&parent=" + parent + "&selectedId=" + selectedId , region.response, "GET", "JSON");
}

region.response = function(res, text_result) {
  if ('undefined' == res || res.error > 0) {
    alert(res.message);
    return false;
  }
  var result = res.content;
  var sel = document.getElementById(result.target);
  sel.length = 1;
  sel.selectedIndex = 0;



  if (document.all) {
    sel.fireEvent("onchange");
  } else {
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent('change', true, true);
    sel.dispatchEvent(evt);
  }

  if (result.regions) {
    if (result.regions.length > 0) {
      sel.innerHTML = '';
      $("<option selected value = ''>--请选择--</option>").appendTo(sel);

      for (i = 0; i < result.regions.length; i++) {
        var opt = document.createElement("OPTION");
        opt.value = result.regions[i].code;
        opt.text = result.regions[i].name;
        if (result.selectedId == opt.value) {
          opt.selected = "selected";
        }
        sel.options.add(opt);
      }
	  sel.style.display = "";
    }
  }
}